package org.jeecg.modules.demo.weixin.smtstoreduserpoint.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.weixin.entity.SmtStoredUserPoint;
import org.jeecg.modules.demo.weixin.entity.SmtValueRecord;
import org.jeecg.modules.demo.weixin.smtstoreduserpoint.mapper.SmtStoredUserPointMapper;
import org.jeecg.modules.demo.weixin.smtstoreduserpoint.service.ISmtStoredUserPointService;
import org.jeecg.modules.demo.weixin.smtvaluerecord.service.ISmtValueRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Description: 用户门店余额
 * @Author: jeecg-boot
 * @Date: 2023-11-17
 * @Version: V1.0
 */
@Service
public class SmtStoredUserPointServiceImpl extends ServiceImpl<SmtStoredUserPointMapper, SmtStoredUserPoint> implements ISmtStoredUserPointService {


    @Autowired
    private ISmtValueRecordService smtValueRecordService;

    /**
     * 用户卡点退款退款功能      type 1 但单表储值退款     2   用户卡点支付的退款
     *
     * @param price
     * @param priceGive
     * @param type
     * @param userId
     * @return
     */
    public Boolean subPrice(BigDecimal price, BigDecimal priceGive, Integer type, Integer userId, String merId, String orderNo) {
        //用户卡点支付逻辑 先查出对应用户卡点
        LambdaQueryWrapper<SmtStoredUserPoint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmtStoredUserPoint::getUserId, userId);
        queryWrapper.eq(SmtStoredUserPoint::getMerchantId, merId);
        SmtStoredUserPoint smtStoredUserPoint = this.getOne(queryWrapper);

        //用户卡点支付的时候
        if (type.equals(2)) {
            if (smtStoredUserPoint == null) {
                SmtStoredUserPoint userPoint = new SmtStoredUserPoint();
                userPoint.setUserId(userId);
                userPoint.setMerchantId(merId);
                this.save(userPoint);
                return Boolean.FALSE;
            }

            //判断余额和和赠送金额是否大于退款金额
            if ((smtStoredUserPoint.getUserRest().add(smtStoredUserPoint.getValueGive())).compareTo(price) < 0) {
                return Boolean.FALSE;
            }
            SmtValueRecord smtValueRecord = new SmtValueRecord();
            //如果用户用户卡点的赠送金额=0的话   直接支付本金
            if (smtStoredUserPoint.getValueGive().compareTo(BigDecimal.valueOf(0)) == 0) {
                smtStoredUserPoint.setUserRest(smtStoredUserPoint.getUserRest().subtract(price));
                smtValueRecord.setPrice(price);
                smtValueRecord.setValuePrice(price);
            }
            //如果用户的储值金额不等于0 的时候
            if (smtStoredUserPoint.getValueGive().compareTo(BigDecimal.valueOf(0)) > 0) {
                //支付的本机是
                BigDecimal principal = price.multiply(smtStoredUserPoint.getUserRest().divide(smtStoredUserPoint.getUserRest().add(smtStoredUserPoint.getValueGive()), BigDecimal.ROUND_CEILING)).setScale(2, 4);
                BigDecimal giveValue = price.subtract(principal);
                smtStoredUserPoint.setUserRest(smtStoredUserPoint.getUserRest().subtract(principal));
                smtStoredUserPoint.setValueGive(smtStoredUserPoint.getValueGive().subtract(giveValue));
                //用户卡点支付记录
                smtValueRecord.setPrice(price);
                smtValueRecord.setValuePrice(principal);
                smtValueRecord.setValueGivePrice(giveValue);

            }
            smtValueRecord.setOrderNo(orderNo);
            smtValueRecordService.save(smtValueRecord);

        }


        this.updateById(smtStoredUserPoint);
        return Boolean.TRUE;
    }

    @Override
    public Boolean addPrice(BigDecimal price, BigDecimal priceGive, Integer type, Integer userId, String merId, String orderNo) {
        LambdaQueryWrapper<SmtStoredUserPoint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmtStoredUserPoint::getUserId, userId);
        queryWrapper.eq(SmtStoredUserPoint::getMerchantId, merId);
        SmtStoredUserPoint storedUserPoint = this.getOne(queryWrapper);

        //用户充值
        if (type.equals(1)) {
            if (storedUserPoint == null) {
                SmtStoredUserPoint userPoint = new SmtStoredUserPoint();
                userPoint.setUserId(userId);
                userPoint.setUserRest(price);
                if (priceGive.compareTo(BigDecimal.valueOf(0)) > 0) {
                    userPoint.setValueGive(priceGive);
                }
                userPoint.setMerchantId(merId);
                this.save(userPoint);
                return Boolean.TRUE;
            }
            storedUserPoint.setUserRest(storedUserPoint.getUserRest().add(price));
            if (priceGive.compareTo(BigDecimal.valueOf(0)) > 0) {
                storedUserPoint.setValueGive(storedUserPoint.getValueGive().add(priceGive));
            }
        }

        //用户卡点支付退款
        if (type.equals(2)) {

            //查询用户卡点支付记录
            LambdaQueryWrapper<SmtValueRecord> smtValueRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
            smtValueRecordLambdaQueryWrapper.eq(SmtValueRecord::getOrderNo, orderNo);
            smtValueRecordLambdaQueryWrapper.eq(SmtValueRecord::getStatus, 0);
            SmtValueRecord recordServiceOne = smtValueRecordService.getOne(smtValueRecordLambdaQueryWrapper);
            if (recordServiceOne == null) {
                return Boolean.FALSE;
            }
            //如果退款金额大于 支付金额
            if (recordServiceOne.getPrice().compareTo(price) < 0) {
                return Boolean.FALSE;
            }
            //如果退款金额等于支付金额  则直接退还
            if (recordServiceOne.getPrice().compareTo(price) == 0) {
                storedUserPoint.setUserRest(storedUserPoint.getUserRest().add(recordServiceOne.getValuePrice()));
                storedUserPoint.setValueGive(storedUserPoint.getValueGive().add(recordServiceOne.getValueGivePrice()));
            }
            //如果退款金额小于支付金额
            if (recordServiceOne.getPrice().compareTo(price) > 0) {
                //如果退款金额没有赠送金额
                if (recordServiceOne.getValueGivePrice().compareTo(BigDecimal.valueOf(0)) == 0) {
                    storedUserPoint.setUserRest(storedUserPoint.getUserRest().add(price));
                }
                if (recordServiceOne.getValueGivePrice().compareTo(BigDecimal.valueOf(0)) > 0) {
                    //如果退款金额小于用户卡点的金额  并且  还有赠送金额
                    BigDecimal refund = price.multiply(recordServiceOne.getValuePrice().divide(recordServiceOne.getValuePrice().add(storedUserPoint.getValueGive()), BigDecimal.ROUND_CEILING)).setScale(2, 4);
                    storedUserPoint.setUserRest(storedUserPoint.getUserRest().add(refund));
                    storedUserPoint.setValueGive(storedUserPoint.getValueGive().add(price.subtract(refund)));
                }
            }

            recordServiceOne.setStatus(1);
            smtValueRecordService.updateById(recordServiceOne);
        }


        this.updateById(storedUserPoint);
        return Boolean.TRUE;
    }


    /**
     * 用户卡点退款退款功能      type 1 但单表储值退款     2   用户卡点支付的退款
     *
     * @param price
     * @param priceGive
     * @param userId
     * @return
     */
    public BigDecimal subPoint(BigDecimal price, BigDecimal priceGive, Integer userId, String merId, String orderNo) {

        //用户卡点支付逻辑 先查出对应用户卡点
        LambdaQueryWrapper<SmtStoredUserPoint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmtStoredUserPoint::getUserId, userId);
        queryWrapper.eq(SmtStoredUserPoint::getMerchantId, merId);
        SmtStoredUserPoint smtStoredUserPoint = this.getOne(queryWrapper);
        //按道理储值退款是一定会有的    因先储值才会去退款的
        if (smtStoredUserPoint == null) {
            return null;
        }

        if (smtStoredUserPoint.getUserRest().compareTo(price) < 0 && smtStoredUserPoint.getUserRest().compareTo(BigDecimal.ZERO) == 0) {
            return null;
        }
        SmtValueRecord smtValueRecord = new SmtValueRecord();

        //充值花费后的退款需要谨慎退款      需要计算是否满足条件金额    满足田间的直接进行扣除   如果不满足条件    扣除花费的赠送   赞进行退还
        //正常情况   购买退款   本金和赠送和购买时的金额相同
        if (smtStoredUserPoint.getUserRest().compareTo(price) >= 0 && smtStoredUserPoint.getValueGive().compareTo(priceGive) >= 0) {
            smtStoredUserPoint.setUserRest(smtStoredUserPoint.getUserRest().subtract(price));
            smtStoredUserPoint.setValueGive(smtStoredUserPoint.getValueGive().subtract(priceGive));
            //添加支付退款记录说明
            smtValueRecord.setPrice(price);
            smtValueRecord.setValuePrice(price);
            smtValueRecord.setValueGivePrice(priceGive);
            smtValueRecord.setRefudValuePrice(price);
            smtValueRecord.setRefundGivePrice(priceGive);
        }
        //现有本金小于充值本金    现有赠送大于 充值赠送  情况下
        if (smtStoredUserPoint.getUserRest().compareTo(price) < 0 && smtStoredUserPoint.getValueGive().compareTo(priceGive) >= 0 && smtStoredUserPoint.getUserRest().compareTo(BigDecimal.ZERO) > 0) {
            smtStoredUserPoint.setValueGive(smtStoredUserPoint.getValueGive().subtract(priceGive));
            //添加支付退款记录说明
            smtValueRecord.setPrice(price);
            smtValueRecord.setValuePrice(price);
            smtValueRecord.setValueGivePrice(priceGive);
            //先赋值   后修改
            smtValueRecord.setRefudValuePrice(smtStoredUserPoint.getUserRest());
            smtStoredUserPoint.setUserRest(BigDecimal.ZERO);
            smtValueRecord.setRefundGivePrice(priceGive);
        }
        //现有本金小于充值本金    先有赠送小于 充值赠送  情况下
        if (smtStoredUserPoint.getUserRest().compareTo(price) < 0 && smtStoredUserPoint.getValueGive().compareTo(priceGive) < 0 && smtStoredUserPoint.getUserRest().compareTo(BigDecimal.ZERO) > 0) {
            //先获取对应的赠送差价
            BigDecimal subtract = priceGive.subtract(smtStoredUserPoint.getValueGive());
            //差价  如果大于现有本金  这个状态不支持 退款
            if (smtStoredUserPoint.getUserRest().compareTo(subtract) < 0) {
                return null;
            }
            //本金小于原本本金  的情况下  需要两边都进行扣除    0 元不回走这边
            smtValueRecord.setRefudValuePrice(smtStoredUserPoint.getUserRest().subtract(subtract));
            smtValueRecord.setRefundGivePrice(smtStoredUserPoint.getValueGive());
            //先赋值在修改
            smtStoredUserPoint.setUserRest(BigDecimal.ZERO);
            smtStoredUserPoint.setValueGive(BigDecimal.ZERO);
            //添加支付退款记录说明
            smtValueRecord.setPrice(price);
            smtValueRecord.setValuePrice(price);
            smtValueRecord.setValueGivePrice(priceGive);

        }
        //现有本金大于充值本金    先有赠送小于 充值赠送  情况下
        if (smtStoredUserPoint.getUserRest().compareTo(price) >= 0 && smtStoredUserPoint.getValueGive().compareTo(priceGive) < 0) {
            //先获取对应的赠送差价
            BigDecimal subtract = priceGive.subtract(smtStoredUserPoint.getValueGive());

            //添加支付退款记录说明
            //大于或者等于原本本金的情况下   之际扣除 原本本金     原本本金减去差价 就是  退回金额
            smtValueRecord.setRefudValuePrice(price.subtract(subtract));
            smtValueRecord.setRefundGivePrice(smtStoredUserPoint.getValueGive());
            //先赋值后修改内容
            smtStoredUserPoint.setUserRest(smtStoredUserPoint.getUserRest().subtract(price));
            smtStoredUserPoint.setValueGive(BigDecimal.ZERO);
            //记录
            smtValueRecord.setPrice(price);
            smtValueRecord.setValuePrice(price);
            smtValueRecord.setValueGivePrice(priceGive);

        }


        this.updateById(smtStoredUserPoint);
        smtValueRecord.setOrderNo(orderNo);
        smtValueRecordService.save(smtValueRecord);
        return smtValueRecord.getRefudValuePrice();
    }
}